/** This program is free software: you can redistribute it and/or modify
    it under the terms of the Lesser GNU General Public License as published by
    the Free Software Foundation, either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with this program. If not, see < http://www.gnu.org/licenses/>.
*/

package dz.borconlib.utils;

/***
 * 
 * @author Mourad TOUMI
 *
 */
public class Converter {
	
	/***
	 * Convertit une chaine de caractaire Hexadecimal vers un tableau de byte 
	 * @param s Chaine Hexadécimale d'entrée
	 * @return Tableau de byte 
	 */
	public static byte[] hexStringToByteArray(String s) {
	    int len = s.length();
	    byte[] data = new byte[len / 2];
	    for (int i = 0; i < len; i += 2) {
	    	
	    	try {
	    		data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                        + Character.digit(s.charAt(i+1), 16));
	    	} catch(StringIndexOutOfBoundsException e) {
	    	}
	        
	    }
	    return data;
	}
	
	/***
	 * Convertit une chaine de caractère en chaine de caractère en représentation Hexadécimale
	 * @param text
	 * @return
	 */
	public static String stringToHexa(String text) {
		int c;
		StringBuffer buff = new StringBuffer(3*text.length());
		for (int i = 0; i < text.length(); i++) {
			c = text.charAt(i);
			buff.append(Integer.toHexString(c));
		}
		return buff.toString();
	}
}
